//
//  WorkOrderAPI.swift
//  Employee
//
//  Created by swordjoy on 2017/3/16.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import Moya

let WorkOrderendpointClosure = { (target: WorkOrderAPI) -> Endpoint<WorkOrderAPI> in
  let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
  return defaultEndpoint.adding(newHTTPHeaderFields: ["X-Requested-With": "XMLHttpRequest", "User-Agent": "iOS", "Cookie": Cookie.cookie])
}

let WorkOrderProvider = MoyaProvider<WorkOrderAPI>(endpointClosure: WorkOrderendpointClosure,
  manager: DefaultAlamofireManager.sharedManager,
  plugins:[
//    NetworkLoggerPlugin(verbose: true)
  ]
)

public enum WorkOrderAPI {
  // 新建工单
  case newOrder(model: NewOrderModel)
  // 获取工单by类型
  case getOrder(status: String, pagepn: String)
  // 获取工单by超时
  case getTimeOutOrder(pagepn: String)
  // 获取工单详情
  case getOrderDetail(id: String)
  // 搜索工单
  case searchOrder(search: String, pagepn: String)
  // 处理工单(接单/转交/完结/回复)
  case handleOrder(type: String, workOrderId: String, staffId: String, replyContent: String, images: String)
  // 提交巡检表单
  case submitInspectionForm(areaId: String, resultData: String)
  // 就一个暂时就放这里了,意见反馈接口
  case feedback(content: String)
  
}

extension WorkOrderAPI: TargetType {
  
  /// The path to be appended to `baseURL` to form the full `URL`.
  public var path: String {
    switch self {
    case .newOrder:
        return "/ms/v\(SJApp.version)/work/order/add.json"
    case .getOrderDetail:
        return "/ms/v\(SJApp.version)/workOrder/detail.json"
    case .handleOrder:
        return "/ms/v\(SJApp.version)/workOrder/handle.json"
    case .getTimeOutOrder:
        return "/ms/v\(SJApp.version)/timeOut.json"
    case .searchOrder:
        return "/ms/v\(SJApp.version)/workOrder/listdata.json"
    case .submitInspectionForm:
        return "/ms/v\(SJApp.version)/inspection/area/form.json"
    case .feedback:
        return "/ms/v\(SJApp.version)/feedback/add.json"
    case .getOrder(let status, _):
        if status == "home" {
            return "/ms/v\(SJApp.version)/workOrder/workList.json"
        }
        return "/ms/v\(SJApp.version)/workOrder/listdata.json"
    }
  }
  
  
  /// The parameters to be incoded in the request.
  public var parameters: [String : Any]? {
    switch self {
    case let .newOrder(model):
      return ["access_token": Token.shared.token, "title": model.title, "content": model.content, "categoryId": model.categoryId, "priorityLevel": model.priorityLevel, "accept": model.accept, "acceptStaffId": model.acceptStaffId, "acceptOrgId": model.acceptOrgId, "imageIds": model.imageIds].signParame(GlobalConfig.privateKey)
    case let .getOrder(status, pagepn):
        return ["access_token": Token.shared.token, "chose": status, "page.pn": pagepn, "page.size": "20", "fields": "id,title,createTime,status,timeout,priorityLevel"].signParame(GlobalConfig.privateKey)
    case let .getTimeOutOrder(pagepn):
      return ["access_token": Token.shared.token, "page.pn": pagepn, "page.size": "20"].signParame(GlobalConfig.privateKey)
    case let .getOrderDetail(id):
      return ["access_token": Token.shared.token, "id": id].signParame(GlobalConfig.privateKey)
    case let .searchOrder(search, pagepn):
      return ["access_token": Token.shared.token, "searchKeyWord": search, "page.pn": pagepn, "page.size": "20", "fields": "id,status,title,createTime,priorityLevel,timeout", "chose": "all"].signParame(GlobalConfig.privateKey)
    case let .handleOrder(type, workOrderId, staffId, replyContent, images):
      return ["access_token": Token.shared.token, "serviceType": type, "id": workOrderId, "staffId": staffId, "content": replyContent, "imageIds": images].signParame(GlobalConfig.privateKey)
    case let .submitInspectionForm(areaId, resultData):
      return ["access_token": Token.shared.token, "areaId": areaId, "result": resultData].signParame(GlobalConfig.privateKey)
    case let .feedback(content):
      return ["access_token": Token.shared.token, "content": content, "dataSource": "PROPERTY_APP"].signParame(GlobalConfig.privateKey)
    }
  }
  
  /// The target's base `URL`.
  public var baseURL: URL {
    return URL(string: GlobalConfig.BASE_OUT_URL)!
  }
  
  /// The HTTP method used in the request.
  public var method: Moya.Method {
    switch self {
    case .newOrder, .submitInspectionForm, .feedback:
      return .post
    case .handleOrder:
      return .put
    default:
      return .get
    }
    
  }
  
  public var task: Task {
    return .request
  }
  
  /// The method used for parameter encoding.
  public var parameterEncoding: ParameterEncoding {
    return URLEncoding.default
  }
  
  // 测试使用,暂时用不上
  public var sampleData: Data {
    return Data()
  }
  
}

